package com.example.blog.service.impl;

import com.example.blog.dao.TypeMapper;
import com.example.blog.pojo.Type;
import com.example.blog.service.TypeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * name: 东北宋小宝
 * date: 2020/8/15
 */
@Service
@Transactional
public class TypeServiceImpl implements TypeService {
    @Resource
    TypeMapper typeMapper;

    @Override
    public List<Type> findAll() {
        return typeMapper.findAll(new Type());
    }

    @Override
    public Type saveType(Type type) throws Exception{
        Type typeMapperType = typeMapper.findType(type);
        if (typeMapperType != null) {
            throw new Exception("标签异常");
        } else {
            typeMapper.saveType(type);
            type = typeMapper.findType(type);
        }
        return type;
    }


    @Override
    public PageInfo<Type> listType(Integer page, Integer num) {
        page = (page == null) ? 1 : page;
        PageHelper.startPage(page, num);
        List<Type> types = typeMapper.findAll(new HashMap(1));
        PageInfo<Type> pageInfo = new PageInfo<Type>(types);
        //PageInfo{pageNum=1, pageSize=5, size=5, startRow=1, endRow=5, total=20, pages=4, list=Page{count=true, pageNum=1, pageSize=5, startRow=0, endRow=5, total=20, pages=4, reasonable=true, pageSizeZero=false}[Type(id=1, name=数学, blogs=[]), Type(id=2, name=数学, blogs=[]), Type(id=3, name=数学, blogs=[]), Type(id=4, name=数学, blogs=[]), Type(id=5, name=数学, blogs=[])], prePage=0, nextPage=2, isFirstPage=true, isLastPage=false, hasPreviousPage=false, hasNextPage=true, navigatePages=8, navigateFirstPage=1, navigateLastPage=4, navigatepageNums=[1, 2, 3, 4]}
        return pageInfo;
    }

    @Override
    public PageInfo<Type> listType(Integer page, Integer num, Long id) {
        Map map = new HashMap(1);
        map.put("id",id);
        PageHelper.startPage(page, num);
        List<Type> all = typeMapper.findAll(map);
        PageInfo<Type> pageInfo = new PageInfo<Type>(all);
        return pageInfo;
    }


    @Override
    public void deleteType(Long id) {
        Map map = new HashMap(1);
        map.put("id",id);
        Type type = typeMapper.findType(map);
        if (type != null){
            typeMapper.deleteType(type);
        }
    }

    @Override
    public Type toEdit(Integer id) {
        Type type = new Type();
        type.setId((long)id);
        return typeMapper.findType(type);
    }

    @Override
    public void edit(Type type) throws Exception{
        Type type1 = new Type();
        BeanUtils.copyProperties(type, type1);
        type.setId(null);
        Type type2 = typeMapper.findType(type);
        if (type2 != null) {
            throw new Exception("标签已存在！");
        } else {
            typeMapper.updateType(type1);
        }
    }

}
